package com.qf.controller;

import com.qf.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/rest")
//@CrossOrigin(value = {"http://localhost:9090", "http://127.0.0.1:9090"})
public class RestfulController {

    List<User> list = new ArrayList<>();

    {
        list.add(new User(1, "zhangsan","123", 18,"男", true, new Date()));
        list.add(new User(2, "lisi","123", 18,"男", true, new Date()));
        list.add(new User(3, "wangwu","123", 18,"男", true, new Date()));
        list.add(new User(4, "maliu","123", 18,"男", true, new Date()));

    }

//    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @GetMapping("/users")
    public List<User> getUsers(){
        //真实开发：从数据库中获取所有的User信息
        return list;
    }


//    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @PostMapping("/users")
    public String saveUser(@RequestBody User user){
        list.add(user);
        System.out.println("Post:"+user);
        return "success";
    }

    @PutMapping("/users")
//    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    public String putUser(@RequestBody User user){
        list.add(user);
        System.out.println("put:"+user);
        return "success";
    }
    //@PathVariable  就路径中的参数映射到方法参数中
    @DeleteMapping("/users/{id}")
//    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable Integer id){
        System.out.println("delete:"+id);

        return "success";
    }
}
